package org.example
import org.apache.spark.sql.{DataFrame, SparkSession}
object data1_SQL1 {
  def main(args: Array[String]): Unit = {
    // 创建SparkSession
    val spark = SparkSession
      .builder()
      .master("local[*]")
      .appName("StudentScoreAnalysis")
      .getOrCreate()
    val studentDF: DataFrame = spark.read
      .option("encoding", "gbk")
      .option("header", "true")
      .csv("C:\\23data01.csv")
    studentDF.createOrReplaceTempView("student_scores")
    val scoreStats = spark.sql(
      """
        |SELECT
        |  ROUND(AVG(`作业(100%)`), 2) AS average_score,
        |  MAX(`作业(100%)`) AS highest_score
        |FROM student_scores
        |""".stripMargin)
    // 2. 找出平时成绩小于60分的学生
    val lowScoreStudents = spark.sql(
      """
        |SELECT
        |  `学生姓名` AS student_name,
        |  `作业(100%)` AS score,
        |  `学号/工号` AS student_id
        |FROM student_scores
        |WHERE `作业(100%)` < 60
        |ORDER BY `作业(100%)` ASC
        |""".stripMargin)
    println("=== 平时成绩统计 ===")
    scoreStats.show()
    println("\n=== 平时成绩低于60分的学生 ===")
    println(s"总计: ${lowScoreStudents.count()}人")
    lowScoreStudents.show(lowScoreStudents.count().toInt, truncate = false)
    spark.stop()
  }




}
